package cn.powerone.scm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.powerone.scm.domain.ScmSupplierQuotesTemplate;
import cn.powerone.scm.upload.domain.ScmImportInquiriesIn;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.powerone.common.annotation.Log;
import cn.powerone.common.core.controller.BaseController;
import cn.powerone.common.core.domain.AjaxResult;
import cn.powerone.common.enums.BusinessType;
import cn.powerone.scm.domain.ScmSupplierQuotes;
import cn.powerone.scm.service.IScmSupplierQuotesService;
import cn.powerone.common.utils.poi.ExcelUtil;
import cn.powerone.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商报价单Controller
 * 
 * @author PowerOne
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/scm/scmSupplierQuotes")
public class ScmSupplierQuotesController extends BaseController
{
    @Autowired
    private IScmSupplierQuotesService scmSupplierQuotesService;

    /**
     * 查询供应商报价单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScmSupplierQuotes scmSupplierQuotes)
    {
        startPage();
        List<ScmSupplierQuotes> list = scmSupplierQuotesService.selectScmSupplierQuotesList(scmSupplierQuotes);
        return getDataTable(list);
    }


    /**
     * 获取供应商报价单详细信息
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scmSupplierQuotesService.selectScmSupplierQuotesById(id));
    }

    /**
     * 新增供应商报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:add')")
    @Log(title = "供应商报价单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScmSupplierQuotes scmSupplierQuotes)
    {
        return toAjax(scmSupplierQuotesService.insertScmSupplierQuotes(scmSupplierQuotes));
    }
    /**
     * 批量新增供应商报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:add')")
    @PostMapping(value = "/{projectId}")
    public AjaxResult create(@PathVariable("projectId") Long projectId)
    {
        return toAjax(scmSupplierQuotesService.createScmSupplierQuotes(projectId));
    }

    /**
     * 修改供应商报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:edit')")
    @Log(title = "供应商报价单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScmSupplierQuotes scmSupplierQuotes)
    {
        return toAjax(scmSupplierQuotesService.updateScmSupplierQuotes(scmSupplierQuotes));
    }

    /**
     * 删除供应商报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:remove')")
    @Log(title = "供应商报价单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scmSupplierQuotesService.deleteScmSupplierQuotesByIds(ids));
    }

    /**
     * Accept状态修改供应商报价单
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:update')")
    @PutMapping("/status/{ids}")
    public AjaxResult updateStatus(@PathVariable Long[] ids, @RequestBody ScmSupplierQuotes scmSupplierQuotes)
    {
        return toAjax(scmSupplierQuotesService.updateStatusScmSupplierQuotesByIds(ids, scmSupplierQuotes));
    }

    /**
     * 导出供应商报价单列表
     */
    @PreAuthorize("@ss.hasPermi('scm:scmSupplierQuotes:export')")
    @Log(title = "供应商报价单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmSupplierQuotes scmSupplierQuotes)
    {
        List<ScmSupplierQuotes> list = scmSupplierQuotesService.selectScmSupplierQuotesList(scmSupplierQuotes);
        // 转换为模板类型
        List<ScmSupplierQuotesTemplate> templateList = list.stream()
                .map(ScmSupplierQuotesTemplate::new)
                .toList();
        ExcelUtil<ScmSupplierQuotesTemplate> util = new ExcelUtil<ScmSupplierQuotesTemplate>(ScmSupplierQuotesTemplate.class);
        util.exportExcel(response, templateList, "供应商报价单数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "询价导入", businessType = BusinessType.IMPORT)
    // @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long projectId) throws Exception
    {
        ExcelUtil<ScmSupplierQuotesTemplate> util = new ExcelUtil<ScmSupplierQuotesTemplate>(ScmSupplierQuotesTemplate.class);
        List<ScmSupplierQuotesTemplate> templateList = util.importExcel(file.getInputStream());
        List<ScmSupplierQuotes> list = new ArrayList<>();
        for (ScmSupplierQuotesTemplate template : templateList) {
            ScmSupplierQuotes quote = new ScmSupplierQuotes();
            template.copyTo(quote);
            list.add(quote);
        }
        String operName = getUsername();
        String message = scmSupplierQuotesService.importData(list, updateSupport, operName,projectId);
        // 异步调用处理数据
//        scmImportInquiriesInService.processImportData();
        return success(message);
    }


    /**
     * 导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ScmSupplierQuotes> util = new ExcelUtil<ScmSupplierQuotes>(ScmSupplierQuotes.class);
        util.importTemplateExcel(response, "供应商报价单");
    }

}
